<?php
// header("Content-type:text/html;charset=utf-8"); 
	//定义一个分页类
	class Page{
		//成员属性
		public $page;//当前页
		public $rows;//数据总条数
		public $pagesize;//当前页大小
		public $maxpage;//最大页
		public $prev;//上一页
		public $next;//下一页
		// 构造方法
		public function __construct($rows,$pagesize){
			$this->pagesize=$pagesize;
			$this->rows=$rows;
			$this->page=isset($_GET['p'])?$_GET['p']:1;//当前页赋值
			$this->checkmax();//检测最大页
			$this->checkpage();//检测当前页

		}

		public function checkmax(){
			$this->maxpage=ceil($this->rows/$this->pagesize);
		}

		public function checkpage(){
			$this->prev=$this->page-1;
			$this->next=$this->page+1;
			if($this->page>=$this->maxpage){
				// $this->page=$this->maxpage;
				$this->next=$this->maxpage;
			}
			if($this->page<=1){
				// $this->page=1;
				$this->prev=1;
			}
		}

		public function limit(){
			return (($this->page-1)*$this->pagesize.','.$this->pagesize);
		}

		//输出方法
		public function show(){
			//获取当前地址
			$url=$_SERVER['PHP_SELF'];
			// echo $url;
			$where=null;
			//遍历
			foreach($_GET as $key=>$value){
				// var_dump($value);
				if(!empty($value)&&$key!='p'){
					$where.="&{$key}={$value}";	
				}
			}
			$str="共{$this->rows}条数据";
			$str.="<a href='{$url}?p=1{$where}'>首页</a>";
			// if($this->page-1!=0){
			// 	$str.="<a href='{$url}?p=".($this->page-1)."{$where}'>上一页</a>";
			// }else{
			// 	$str.="<a href='{$url}?p=".($this->page)."{$where}'>上一页</a>";

			// }
			// if($this->page+1>$this->maxpage){
			// 	$str.="<a href='{$url}?p=".($this->page)."{$where}'>下一页</a>";
			// }else{
			// 	$str.="<a href='{$url}?p=".($this->page+1)."{$where}'>下一页</a>";

			// }
			// $str.="<a href='{$url}?p=".($this->page-1<1?1:($this->page-1))."{$where}'>上一页</a>";
			// $str.="<a href='{$url}?p=".($this->page+1>$this->maxpage?$this->maxpage:$this->page+1)."{$where}'>下一页</a>";
			$str.="<a href='{$url}?p=".($this->prev)."{$where}'>上一页</a>";

			$str.="<a href='{$url}?p=".($this->next)."{$where}'>下一页</a>";

			$str.="<a href='{$url}?p={$this->maxpage}{$where}'>末页</a>";
			return $str;
		}

	} 
 ?>